/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.spring;

import com.jakewharton.nopen.annotation.Open;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import io.sentry.IHub;
import io.sentry.protocol.Request;
import io.sentry.util.Objects;

/**
 * SentryRequestResolver
 *
 * @since 2021-07-29
 */
@Open
public class SentryRequestResolver {
    private static final List<String> SENSITIVE_HEADERS =
        Arrays.asList("X-FORWARDED-FOR", "AUTHORIZATION", "COOKIE");

    private final @NotNull IHub hub;

    /**
     * SentryRequestResolver
     *
     * @param hub IHub
     */
    public SentryRequestResolver(final @NotNull IHub hub) {
        this.hub = Objects.requireNonNull(hub, "options is required");
    }

    /**
     * resolveSentryRequest
     *
     * @param httpRequest HttpServletRequest
     * @return Request
     */
    @SuppressWarnings("JdkObsolete")
    public @NotNull Request resolveSentryRequest(final @NotNull HttpServletRequest httpRequest) {
        final Request sentryRequest = new Request();
        sentryRequest.setMethod(httpRequest.getMethod());
        sentryRequest.setQueryString(httpRequest.getQueryString());
        sentryRequest.setUrl(httpRequest.getRequestURL().toString());
        sentryRequest.setHeaders(resolveHeadersMap(httpRequest));

        if (hub.getOptions().isSendDefaultPii()) {
            sentryRequest.setCookies(toString(httpRequest.getHeaders("Cookie")));
        }
        return sentryRequest;
    }

    /**
     * resolveHeadersMap
     *
     * @param request HttpServletRequest
     * @return Map<String, String>
     */
    @NotNull
    Map<String, String> resolveHeadersMap(final @NotNull HttpServletRequest request) {
        final Map<String, String> headersMap = new HashMap<>();
        for (String headerName : Collections.list(request.getHeaderNames())) {
            // do not copy personal information identifiable headers
            if (hub.getOptions().isSendDefaultPii()
                || !SENSITIVE_HEADERS.contains(headerName.toUpperCase(Locale.ROOT))) {
                headersMap.put(headerName, toString(request.getHeaders(headerName)));
            }
        }
        return headersMap;
    }

    private static @Nullable String toString(final @Nullable Enumeration<String> enumeration) {
        return enumeration != null ? String.join(",", Collections.list(enumeration)) : null;
    }
}
